package com.ww.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ww.entity.WallpaperUpload;
import com.ww.mapper.WallpaperUploadDao;
import com.ww.pojo.Wallpaper;
import com.ww.mapper.WallpaperDao;
import com.ww.pojo.WallpaperComment;
import com.ww.pojo.WallpaperVO;
import com.ww.service.WallpaperService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * (Wallpaper)表服务实现类
 *
 * @author makejava
 * @since 2022-05-18 17:34:13
 */
@Slf4j
@Service("wallpaperService")
public class WallpaperServiceImpl implements WallpaperService {
    @Resource
    private WallpaperDao wallpaperDao;
    @Resource
    private WallpaperUploadDao wallpaperUploadDao;

    /**
     * 通过ID查询单条数据
     *
     * @param wallpaperId 主键
     * @return 实例对象
     */
    @Override
    public Wallpaper queryById(Integer wallpaperId) {
        return this.wallpaperDao.queryById(wallpaperId);
    }


    /**
     * 查询壁纸详情
     * @param wallpaperId
     * @return
     */
    @Override
    public WallpaperVO selectbyid(Integer wallpaperId) {
        WallpaperVO wallpaperVO=this.wallpaperDao.wallpapervoselect(wallpaperId);
        List<String> labellist =this.wallpaperDao.selectlabel(wallpaperId);
        if (labellist!=null&&labellist.size()!=0) wallpaperVO.setLabellist(labellist);
        List<WallpaperComment> commentList =this.wallpaperDao.selectcomment(wallpaperId);
        if (commentList!=null&&commentList.size()!=0&&commentList.get(0)!=null) wallpaperVO.setCommentList(commentList);
        return wallpaperVO;
    }

    /**
     * 分页查询壁纸
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Wallpaper> wallpaperpage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Wallpaper> findallwallpaper = wallpaperDao.findallwallpaper();
        return new PageInfo<>(findallwallpaper);
    }

    /**
     * 分页查询壁纸（未审核）
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Wallpaper> wallpaperpageBystate(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Wallpaper> findallwallpaper = wallpaperDao.findallwallpaperBystate();
        return new PageInfo<>(findallwallpaper);
    }

    /**
     * 分页查询壁纸（审核通过）
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Wallpaper> wallpaperpageByTG(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Wallpaper> findallwallpaper = wallpaperDao.findallwallpaperByTG();
        return new PageInfo<>(findallwallpaper);
    }

    /**
     * 分页查询壁纸（条件查询）
     * @param pageNum
     * @param condition
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Wallpaper> wallpaperpagevague(Integer pageNum, String condition, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Wallpaper> wallpaperList = wallpaperDao.findallwallpapervague1(condition);
        wallpaperList.addAll(wallpaperDao.findallwallpapervague2(condition));
        wallpaperList.addAll(wallpaperDao.findallwallpapervague3(condition));
        return new PageInfo<>(wallpaperList);
    }

    /**
     * 分页查询
     *
     * @param wallpaper   筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<Wallpaper> queryByPage(Wallpaper wallpaper, PageRequest pageRequest) {
        long total = this.wallpaperDao.count(wallpaper);
        return new PageImpl<>(this.wallpaperDao.queryAllByLimit(wallpaper, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param wallpaperVO 实例对象
     * @return 实例对象
     */
    @Override
    public WallpaperVO insert(WallpaperVO wallpaperVO, HttpSession httpSession) {
        //httpSession.setAttribute("userId","14");
        Wallpaper wallpaper=new Wallpaper();
        wallpaper.setWallpaperName(wallpaperVO.getWallpaperName());
        wallpaper.setWallpaperUrl(wallpaperVO.getWallpaperUrl());
        wallpaper.setWallpaperThumb(wallpaperVO.getWallpaperThumb());
        this.wallpaperDao.insert(wallpaper);
        WallpaperUpload wallpaperUpload = new WallpaperUpload();
        wallpaperUpload.setWallpaperId(wallpaper.getWallpaperId());
        wallpaperUpload.setUploadTime(new Date());
        wallpaperUpload.setUserId(Integer.parseInt(httpSession.getAttribute("userId").toString()));
        this.wallpaperUploadDao.insert(wallpaperUpload);
        for (String label:wallpaperVO.getLabellist()){
            this.wallpaperDao.insertlabel(wallpaper.getWallpaperId(),label);
        }
        return wallpaperVO;
    }

    @Override
    public WallpaperComment insertcomment(WallpaperComment wallpaperComment, HttpSession httpSession) {
        wallpaperComment.setUserId(Integer.parseInt(httpSession.getAttribute("userId").toString()));
        wallpaperComment.setCommentTime(new Date());
        this.wallpaperDao.insertcomment(wallpaperComment);
        return wallpaperComment;
    }


    /**
     * 修改数据
     *
     * @param wallpaper 实例对象
     * @return 实例对象
     */
    @Override
    public Wallpaper update(Wallpaper wallpaper) {
        this.wallpaperDao.update(wallpaper);
        return this.queryById(wallpaper.getWallpaperId());
    }

    /**
     * 通过主键删除数据
     *
     * @param wallpaperId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer wallpaperId) {
        return this.wallpaperDao.deleteById(wallpaperId) > 0;
    }

    //@Override
    //public void testinsert() {
    //    for (int i=22;i<86;i++) {
    //        System.out.println("添加了壁纸编号为"+i+"的上传记录");
    //        WallpaperUpload wallpaperUpload = new WallpaperUpload();
    //        wallpaperUpload.setWallpaperId(i);
    //        wallpaperUpload.setUploadTime(new Date());
    //        wallpaperUpload.setUserId(14);
    //        this.wallpaperUploadDao.insert(wallpaperUpload);
    //    }
    //}
}
